package com.examiner.service.impl;

import com.examiner.bean.test.*;
import com.examiner.bean.vo.CommitMyTestV0;
import com.examiner.bean.vo.MyTestAndTestDBVo;
import com.examiner.bean.vo.NewTestDB;
import com.examiner.bean.vo.NewTestType;
import com.examiner.dao.*;
import com.examiner.page.QueryInfo;
import com.examiner.service.MyTestService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class MyTestServiceImpl implements MyTestService {

    @Resource
    private NewTestUserDao newTestUserDao;

    @Resource
    private TestDBDao testDBDao;

    @Resource
    private NewTestDao newTestDao;

    @Resource
    private TestTypeDao testTypeDao;

    @Resource
    private NewTestItemDao newTestItemDao;

    /**
     * 根据ID获取当前用户的考评记录
     * @param id
     * @param queryInfo
     * @return
     */
    @Override
    public List<MyTestAndTestDBVo> getMyTestList(Integer id, QueryInfo queryInfo) throws Exception {
        List<MyTestAndTestDBVo> list = new ArrayList<>();
        MyTestAndTestDBVo myTestAndTestDBVo;
        List<NewTestUser> myTestList = newTestUserDao.findMyTestList(id, (queryInfo.getPageNum() - 1) * queryInfo.getPageSize(), queryInfo.getPageSize(), queryInfo.getQuery());
        if (myTestList.size() != 0){
            for (NewTestUser newTestUser : myTestList) {
                myTestAndTestDBVo = new MyTestAndTestDBVo();
                TestDB one = testDBDao.getOne(newTestUser.getTid());
                NewTest one1 = newTestDao.getOne(newTestUser.getNid());
                myTestAndTestDBVo.setTestDB(one);
                myTestAndTestDBVo.setNewTestUser(newTestUser);
                myTestAndTestDBVo.setNewTest(one1);
                list.add(myTestAndTestDBVo);
            }
        }
        return list;
    }

    /**
     * 根据ID获取当前用户的考评记录总条数
     * @param id
     * @param queryInfo
     * @return
     * @throws Exception
     */
    @Override
    public int getMyTestListTotal(Integer id, QueryInfo queryInfo) throws Exception {
        return newTestUserDao.findMyListTotal(id,queryInfo.getQuery());
    }

    /**
     * 获取考评信息
     * @param nid
     * @return
     */
    @Override
    public NewTestDB getMyTestInfo(Integer nid) throws Exception {
        NewTestType newTestType;
        List<NewTestType> newTestTypes = new ArrayList<>();
        NewTestDB newTestDB = new NewTestDB();
        NewTestUser newTestUser = newTestUserDao.getOne(nid);
        TestDB testDB = testDBDao.getOne(newTestUser.getTid());
        List<TestType> testTypes = testTypeDao.findBySid(testDB.getTestId());
        if (testTypes.size() != 0){
            for (TestType testType : testTypes) {
                newTestType = new NewTestType();
                BeanUtils.copyProperties(testType,newTestType);
               List<NewTestItem> newTestItems =  newTestItemDao.findByNtuidAndSid(newTestUser.getId(),testType.getTestTypeId());
               if (newTestItems.size() != 0){
                   newTestType.setNewTestItems(newTestItems);
               }
               newTestTypes.add(newTestType);
            }
        }
        BeanUtils.copyProperties(testDB,newTestDB);
        newTestDB.setTestTypes(newTestTypes);
        return newTestDB;
    }

    /**
     * 获取当前考评记录
     * @param id
     * @return
     * @throws Exception
     */
    @Override
    public NewTestUser getNewTestUser(Integer id) throws Exception{
        return newTestUserDao.getOne(id);
    }

    /**
     * 提交考评信息
     * @param commitMyTestV0
     * @throws Exception
     */
    @Override
    @Transactional
    public void commintMyTest(CommitMyTestV0 commitMyTestV0) throws Exception {
        NewTestItem item;
        // 更新用户考评记录
        NewTestUser newTestUser = newTestUserDao.getOne(commitMyTestV0.getNewTestUser().getId());
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        newTestUser.setCommitTime(formatter.format(new Date()));
        newTestUser.setState(1);
        newTestUser.setMyTotalGrade(commitMyTestV0.getNewTestUser().getMyTotalGrade());
        newTestUserDao.save(newTestUser);
        // 保存考评项的数据
        NewTestDB newTestDB = commitMyTestV0.getNewTestDB();
        for (NewTestType testType : newTestDB.getTestTypes()) {
            for (NewTestItem newTestItem : testType.getNewTestItems()) {
                item = newTestItemDao.getOne(newTestItem.getId());
                item.setMyGrade(newTestItem.getMyGrade());
                newTestItemDao.save(item);
            }
        }
    }

    /**
     * 提交审核信息
     * @param commitMyTestV0
     * @throws Exception
     */
    @Transactional
    @Override
    public void commintCheckTest(CommitMyTestV0 commitMyTestV0) throws Exception {
        NewTestItem item;
        // 更新用户考评记录
        NewTestUser newTestUser = newTestUserDao.getOne(commitMyTestV0.getNewTestUser().getId());
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        newTestUser.setExaminerCommitTime(formatter.format(new Date()));
        newTestUser.setState(2);
        newTestUser.setExaminerTotalGrade(commitMyTestV0.getNewTestUser().getExaminerTotalGrade());
        newTestUserDao.save(newTestUser);
        // 保存考评项的数据
        NewTestDB newTestDB = commitMyTestV0.getNewTestDB();
        for (NewTestType testType : newTestDB.getTestTypes()) {
            for (NewTestItem newTestItem : testType.getNewTestItems()) {
                item = newTestItemDao.getOne(newTestItem.getId());
                item.setExaminerGrade(newTestItem.getExaminerGrade());
                newTestItemDao.save(item);
            }
        }
    }
}
